//
//  FunctionDemandHeadView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/28.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "FunctionDemandHeadView.h"
#import "ReleaseModel.h"
#import "SubjectRecommendModel.h"
@interface FunctionDemandHeadView()
@property(nonatomic,strong) UIButton *mcBtn;
@property(nonatomic,strong) UIButton *preMCBtn;
@property(nonatomic,assign) CGFloat w;
@property(nonatomic,assign) NSInteger maxSubBtnX;
@property(nonatomic,strong) NSMutableArray *titles;
@property(nonatomic,strong) NSMutableArray *subTitles;
@property(nonatomic,strong) UIScrollView *scrollView;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@end

@implementation FunctionDemandHeadView
-(NSMutableArray *)titles
{
    if (!_titles) {
        _titles = [NSMutableArray array];
    }
    return _titles;
}
-(NSMutableArray *)subTitles
{
    if (!_subTitles) {
        _subTitles = [NSMutableArray array];
    }
    return _subTitles;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.manager = [AFHTTPSessionManager manager];
        self.backgroundColor = [UIColor whiteColor];
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, kWidth, kHeight)];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.scrollView];
//        self.scrollView.backgroundColor = [UIColor whiteColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiulistlistByFatherId:) name:@"qingqiulistlistByFatherId" object:nil];
//        [self reloadInternetData];
    }
    return self;
}

-(void)setSubjectModel:(SubjectRecommendModel *)subjectModel
{
    _subjectModel = subjectModel;
    NSString *fatherId = [NSString stringWithFormat:@"%ld",subjectModel.id];
    NSDictionary *dictFather = @{
                                 @"fatherId":fatherId
                                 };
    XLog(@"dictFather:%@",dictFather);
    [CustomeManager POSTWithURLIndex:11 parameters:dictFather success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)reloadInternetData
{
  
    
}
-(void)qingqiulistlistByFatherId:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSArray *date = [dict objectForKey:@"date"];
//    for (NSDictionary *dict in date) {
//        ReleaseModel *model = [ReleaseModel mj_objectWithKeyValues:dict];
//        [self.titles addObject:model];
//    }
    for (NSDictionary *dict in date) {
        ReleaseModel *model = [ReleaseModel mj_objectWithKeyValues:dict];
        [self.subTitles addObject:model];
    }
    NSDictionary *dictFirst = @{
                           @"content":@"全部"
                           };
    ReleaseModel *modelFirst = [ReleaseModel mj_objectWithKeyValues:dictFirst];
    [self.subTitles insertObject:modelFirst atIndex:0];
//    XLog(@"self.titles:%@",self.titles);
//    //使用GCD的信号量 dispatch_semaphore_t 创建同步请求
//    dispatch_group_t group =dispatch_group_create();
//    dispatch_queue_t globalQueue=dispatch_get_global_queue(0, 0);
//    dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
//    dispatch_group_async(group, globalQueue, ^{
//        for (int i = 0; i<self.titles.count; i++) {
//            ReleaseModel *model = self.titles[i];
//            NSString *fatherId = [NSString stringWithFormat:@"%ld",model.id];
//            NSDictionary *dictFather = @{
//                                         @"fatherId":fatherId
//                                         };
//            XLog(@"dictFather:%@",dictFather);
//            self.urlString = [NSString stringWithFormat:@"%@/associationTypeSub/listByFatherId",kUrl];
//            [self.manager POST:self.urlString parameters:dictFather progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//                XLog(@"HomeresponseObject:%@",responseObject);
//                NSArray *date = [responseObject objectForKey:@"date"];
//                for (NSDictionary *dict in date) {
//                    ReleaseModel *model = [ReleaseModel mj_objectWithKeyValues:dict];
//                    [self.subTitles addObject:model];
//                }
//
//                dispatch_semaphore_signal(semaphore);
//            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//                XLog(@"error:%@",error);
//                dispatch_semaphore_signal(semaphore);
//            }];
//            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//        }
//
//    });
    
//    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
//        XLog(@"%@---全部结束。。。",[NSThread currentThread]);
//        dispatch_async(dispatch_get_main_queue(), ^{
//            XLog(@"self.subTitles:%@",self.subTitles);
//            [WSProgressHUD dismiss];
            [self setUpUI];
//        });
//    });
}
-(void)setUpUI
{
    for (int i = 0; i < self.subTitles.count; i++) {
        ReleaseModel *model = self.subTitles[i];
        self.w = [MCButton getWidthWithTitle:model.content withFontSize:14]+30;
        //        XLog(@"self.w:%f",self.w);
        if (i == 0) {
            self.maxSubBtnX = 0;
        }else {
            self.maxSubBtnX = CGRectGetMaxX(self.mcBtn.frame);
        }
        self.mcBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.maxSubBtnX+15, 8, self.w, 24)];
        [self.scrollView addSubview:self.mcBtn];
        [self.mcBtn setTitle:model.content forState:UIControlStateNormal];
        self.mcBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        self.mcBtn.layer.masksToBounds = YES;
        self.mcBtn.layer.cornerRadius = 3;
        //        self.subBtn.layer.borderColor  = kLabel244Color.CGColor;
        //        self.subBtn.layer.borderWidth  = 1;
        
        self.mcBtn.tag = i+1;
        [self.mcBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            //            [self.subBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            self.mcBtn.backgroundColor = kOrangeBg;
            [self.mcBtn setTitleColor:kWhite forState:UIControlStateNormal];
            self.preMCBtn = self.mcBtn;
            
        }else{
            //            [self.subBtn setImage:[UIImage new] forState:UIControlStateNormal];
            [self.mcBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
            self.mcBtn.backgroundColor = kLabel244Color;
        }
    }
    self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(self.mcBtn.frame)+18, 0);
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)clickBtn:(UIButton *)clickBtn
{
    if (self.preMCBtn == clickBtn) {
        clickBtn.backgroundColor = kOrangeBg;
        [clickBtn setTitleColor:kWhite forState:UIControlStateNormal];
    }else {
        clickBtn.backgroundColor = kOrangeBg;
        [clickBtn setTitleColor:kWhite forState:UIControlStateNormal];
        [self.preMCBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
        self.preMCBtn.backgroundColor = kLabel244Color;
    }
    self.preMCBtn = clickBtn;
    if ([self.delegate respondsToSelector:@selector(functionDemandHeadView:withBtn:)]) {
        [self.delegate functionDemandHeadView:self withBtn:clickBtn];
    }
}
@end
